<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>methods</title>
    <script src="../assets/js/vue.js"></script>
</head>
<body>
    <h1>methods</h1>
    <hr/>
    <div id="app">
        <p>{{count}}</p>
        <p><button @click="add(2,$event)">增加</button></p>
        <!--组件中调用方法需要加上.native修饰符-->
        <p><btn @click.native="add()"></btn></p>
    </div>
    <button onclick="app.add(3);">外部的按钮</button>

    <script>

        var btn = {
            template: `<button>组件的按钮</button>`
        }

        var app = new Vue({
            el: '#app',
            data: {
                //这里使用null,也可以
                count: 0
            },
            methods: {
                //这里num=null和num=''都可以使用if(num)来判断
                add: function(num,event){
                    if(num){
                        this.count += num;
                    }else{
                    this.count++;
                    }
                    console.log(event);
                }
            },
            components:{
                "btn": btn
            }
        });
    </script>

</body>
</html>